/**************************************************
*
*	ListToList控件。
*	
**************************************************/
WebbxListToListComp.prototype = new BaseComponent;
WebbxListToListComp.BUTTSWIDTH = 16;
WebbxListToListComp.BUTTSHEIGHT = 16;

/**
*	WebbxListToListComp构造函数
*/
function WebbxListToListComp(parent, name, left, top, width, height, position, leftLabelName, rightLabelName, withAll)
{
	if(arguments.length == 0)
		return;
		
    this.base = BaseComponent;
    this.base(name, left, top, width, height);
    
    this.position = getString(position, "absolute");
    this.parentOwner = parent;
    this.leftLabelName = getString(leftLabelName, trans("ml_allchooseablevalues"));
    this.rightLabelName = getString(rightLabelName, trans("ml_allchoosedvalues"));
    this.withAll = withAll;
    this.create();
};

/**
*	WebbxListToListComp的主体创建函数
*/
WebbxListToListComp.prototype.create = function()
{	
	this.Div_gen = $ce("DIV");
	this.Div_gen.id = this.name;
	this.Div_gen.style.width = this.width;
	this.Div_gen.style.height = this.height;
	this.Div_gen.style.left = this.left + "px";
	this.Div_gen.style.top = this.top + "px";
	this.Div_gen.style.position = this.position;
	if (this.parentOwner) 
	{	 
    	this.placeIn(this.parentOwner);
    }     
};

WebbxListToListComp.prototype.setLeftComp = function(leftDiv){
	this.leftPanel.appendChild(leftDiv);
};

WebbxListToListComp.prototype.setRightComp = function(rightDiv){
	this.rightPanel.appendChild(rightDiv);
};

WebbxListToListComp.prototype.formRightData = function() {
	return null;
};

WebbxListToListComp.prototype.formAllRightData = function() {
	return null;
};

WebbxListToListComp.prototype.rightToLeft = function() {
	var selIndices = this.rightDataset.getSelectedIndices();
	if(selIndices == null)
		return;
	var indices = new Array;
	for(var i = 0; i < selIndices.length; i ++)
		indices.push(selIndices[i]);
	this.rightDataset.deleteRows(indices);
};

WebbxListToListComp.prototype.rightToLeftAll = function() {
	this.rightDataset.clearData(null);
};

WebbxListToListComp.prototype.setRightDataset = function(ds, identicalCol) {
	this.rightDataset = ds;
	this.rightDataset.initialize();
	this.identicalCol = identicalCol;
};

WebbxListToListComp.prototype.afterAddRightRow = function(row) {
	
};

/**
*	回掉函数,具体构造listToList中的所有组件
*/                    
WebbxListToListComp.prototype.manageSelf = function()
{	
	var oThis = this;
	
	var totalWidth = this.Div_gen.offsetWidth;
	var totalHeight = this.Div_gen.offsetHeight;
	//parent, name, left, top, width, height, position, scroll, className
	//存放目前选中的items
	this.changeValues = new Array();
	
	//在最右侧再添加4个按钮,所以为了不重新计算,把width减去50,为最右边的按钮留出位置
	var cw = totalWidth;
	
	var listWidth = (cw - 36)/2 - 20;
	var listHeight = totalHeight - 20 - 20 - 40;
	var listTop = (totalHeight - listHeight)/2;
	
	//计算左list的left值
	var leftPanelLeft = ((cw - 40)/2 - listWidth)/2;
	//计算左label的left值
	this.leftLabel = new LabelComp(this.Div_gen, "leftlabel", leftPanelLeft, 10, this.leftLabelName, "");
	//计算右侧容器panel的left值
	var rightPanelLeft = 80 + (cw - 80)/2 + leftPanelLeft - 20;
	// 构造右容器div,树或者list均放于此容器内
	this.rightPanel = $ce("div");
	this.rightPanel.style.position = "absolute";
	this.rightPanel.style.left = rightPanelLeft;
	this.rightPanel.style.top = listTop;
	this.rightPanel.style.width = listWidth;
	this.rightPanel.style.height = listHeight;
	this.rightPanel.style.overflow = "hidden";
	this.rightPanel.style.border = "1px solid gray";
	this.Div_gen.appendChild(this.rightPanel);
	
	// 构造左容器div,树或者list均放于此容器内
	this.leftPanel = $ce("div");
	this.leftPanel.style.position = "absolute";
	this.leftPanel.style.left = leftPanelLeft;
	this.leftPanel.style.top = listTop;
	this.leftPanel.style.width = listWidth;
	this.leftPanel.style.height = listHeight;
	this.leftPanel.style.overflow = "hidden";
	this.leftPanel.style.border = "1px solid gray";
	this.Div_gen.appendChild(this.leftPanel);
	

	//计算右label的left值
	var labelRightLeft = rightPanelLeft;
	this.rightLabel = new LabelComp(this.Div_gen, "rightlabel", labelRightLeft, 10 , this.rightLabelName, "absolute", null);
	
	var buttLeft = listWidth + 30;
	var buttBaseTop = Math.round((listHeight - 94)/2) + listTop;
	var buttInternal = 10;
	
	/*--------------------------------------
		点击左边的项后将左边的项打到右边 
	*--------------------------------------*/ 
	//this.oneToRight = new ButtonComp(this.Div_gen, "oneToRight", buttLeft, listTop, WebbxListToListComp.BUTTSWIDTH, WebbxListToListComp.BUTTSHEIGHT, "向右", "向右", "", "", "");
	this.oneToRight = new ImageComp(this.Div_gen, "oneToRight", window.themePath + "/images/listtolist/torightnormal.gif", buttLeft, buttBaseTop, "", "", trans("ml_toright"), window.themePath + "/images/listtolist/torighthot.gif", "");
	this.oneToRight.onclick = function()
	{	
		var newRows = oThis.formRightData();
		oThis.transRows(newRows);
	};
	
	/*--------------------------------------
		将左边的items全部移到右边
	*--------------------------------------*/	
	if(this.withAll){
		//this.allToRight = new ButtonComp(this.Div_gen, "allToRight", buttLeft, listTop + 2*WebbxListToListComp.BUTTSHEIGHT + 2*buttInternal, WebbxListToListComp.BUTTSWIDTH, WebbxListToListComp.BUTTSHEIGHT, ml_alltoright, "", "", "", "");
		this.allToRight = new ImageComp(this.Div_gen, "allToRight", window.themePath + "/images/listtolist/alltorightnormal.gif", buttLeft, buttBaseTop + WebbxListToListComp.BUTTSHEIGHT + buttInternal, "", "", trans("ml_alltoleft"), window.themePath + "/images/listtolist/alltorighthot.gif", "");
		this.allToRight.onclick = function()
		{
			//oThis.rightDataset.clearData(null);
			var allRows = oThis.formAllRightData();
			oThis.transRows(allRows);
		};
	}
	/*--------------------------------------
		点击右边的一个项后将右边的打到左边
	*--------------------------------------*/ 	
	//this.oneToLeft = new ButtonComp(this.Div_gen, "oneToLeft", buttLeft, listTop + WebbxListToListComp.BUTTSHEIGHT + buttInternal, WebbxListToListComp.BUTTSWIDTH, WebbxListToListComp.BUTTSHEIGHT, ml_toleft, "", "", "", "");
	this.oneToLeft = new ImageComp(this.Div_gen, "oneToLeft", window.themePath + "/images/listtolist/toleftnormal.gif", buttLeft, buttBaseTop + 2*WebbxListToListComp.BUTTSHEIGHT + 2*buttInternal, "", "", trans("ml_toleft"), window.themePath + "/images/listtolist/tolefthot.gif", "");
	this.oneToLeft.onclick = function()
	{	
		oThis.rightToLeft();
	};
	
	/*--------------------------------------
		将右边的items全部移到左边
	*--------------------------------------*/
	if(this.withAll){
		//this.allToLeft = new ButtonComp(this.Div_gen, "allToLeft", buttLeft, listTop +  3*WebbxListToListComp.BUTTSHEIGHT + 3*buttInternal, WebbxListToListComp.BUTTSWIDTH, WebbxListToListComp.BUTTSHEIGHT, ml_alltoleft, "", "", "", "");
		this.allToLeft = new ImageComp(this.Div_gen, "allToLeft", window.themePath + "/images/listtolist/alltoleftnormal.gif", buttLeft, buttBaseTop +  3*WebbxListToListComp.BUTTSHEIGHT + 3*buttInternal, "", "", trans("ml_alltoleft"), window.themePath + "/images/listtolist/alltolefthot.gif", "");
		this.allToLeft.onclick = function()
		{	
			oThis.rightToLeftAll();
		};
	}
};

WebbxListToListComp.prototype.transRows = function(newRows) {
	if(newRows != null && newRows.length > 0){
		var identicalIndex = this.rightDataset.nameToIndex(this.identicalCol);
		var oldRows = this.rightDataset.getRows();
		for(var i = 0; i < newRows.length; i ++){
			var row = newRows[i];
			var exist = false;
			for(var j = 0; oldRows != null && j < oldRows.length; j ++){
				if(oldRows[j].getCellValue(identicalIndex) == row.getCellValue(identicalIndex)){
					exist = true;
					break;
				}
			}
			if(!exist){
				this.rightDataset.addRow(row);
				this.afterAddRightRow(row);
			}
		}
	}
};

